/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ntb.controllers;

import com.ntb.common.SecuredAction;
import com.ntb.controllers.forms.LandPlanForm;
import com.ntb.entities.LandPlansLaid;
import com.ntb.models.LandPlansLaidDAO;
import com.ntb.models.impl.LandPlansLaidDAOImpl;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class LandPlanAction extends SecuredAction {

    @Override
    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        
        ActionForward forward = mapping.getInputForward();
        LandPlanForm landPlanForm = (LandPlanForm) form;
        LandPlansLaidDAO landPlanDao = new LandPlansLaidDAOImpl();
        String action = landPlanForm.getAction();

        List<String> errorMessages = new ArrayList();
        List<String> successMessages = new ArrayList();
        if ("save".equalsIgnoreCase(action)) {
            LandPlansLaid landPlan = new LandPlansLaid();

            landPlan.setPlanID(landPlanForm.getPlanID());
            landPlan.setPlanName(landPlanForm.getPlanName());

            if (landPlan.getPlanID() > 0) {
                LandPlansLaid temp = landPlanDao.getRow(landPlan.getPlanID());
                if (temp == null) {
                    errorMessages.add("Land Plan not found!");
                } else {
                    boolean available = true;

                    if (!temp.getPlanName().equalsIgnoreCase(landPlan.getPlanName())) {
                        if (landPlanDao.checkDuplicateName(landPlan.getPlanName())) {
                            available = false;
                            errorMessages.add("The land plan name you selected is already in use.");
                            forward = mapping.findForward("landPlanEdit");
                        }
                    }

                    if (available) {
                        landPlanDao.update(landPlan);
                        successMessages.add("The land plan \"" + landPlan.getPlanName() + "\" has been saved.");
                        forward = mapping.findForward("landPlanListing");
                    }
                }
            } else {
                boolean available = true;
                if (landPlanDao.checkDuplicateName(landPlan.getPlanName())) {
                    available = false;
                    errorMessages.add("The land plan name you selected is already in use.");
                }

                if (available) {
                    landPlanDao.add(landPlan);
                    successMessages.add("The land plan \"" + landPlan.getPlanName() + "\" has been added.");
                    forward = mapping.findForward("landPlanListing");
                }
            }
        } else if (landPlanForm.getPlanID() > 0) {
            LandPlansLaid landPlan = landPlanDao.getRow(landPlanForm.getPlanID());

            landPlanForm.setPlanID(landPlan.getPlanID());
            landPlanForm.setPlanName(landPlan.getPlanName());

            forward = mapping.findForward("landPlanEdit");
        } else {
            landPlanForm.setPlanID(0);
        }

        request.setAttribute("errorMessages", errorMessages);
        request.setAttribute("successMessages", successMessages);

        return forward;
    }

}
